package output;

import java.util.List;
import java.util.ArrayList;

import org.w3c.dom.Element;

import ontology.Ontology;
import ontology.FrameRelationType;
import ontology.FrameRelation;

/** 
 * The role of this class is to implement the 'semantictype' elements 
 * in harvested lexical entries. The format of these elements is 
 * recursive since each type can have more than one parent type:
 * 
 *   semantictype @name
 *     > semantictype*
 *     
 * The constructor method creates a semantic type tree with a particular 
 * type as root, and then consults the ontology to build up the 
 * superstructure recursively. 
 */

public class SemanticType {

	public String name;
	public List<SemanticType> superTypes = new ArrayList<SemanticType>();
	
	public SemanticType(String s,Ontology o) {
		name = s;
		for (FrameRelationType r: o.relations) {
			if (r.name.equals("Inheritance")) {
				for (FrameRelation l: r.links) {
					if (l.subFrameName.equals(this.name)) {
						superTypes.add(new SemanticType(l.superFrameName,o));
					}
				}
				break;
			}
		}
	}
	
	public SemanticType(Element e) {
		name = e.getAttribute("name");
	}
	
	public String toXML(int n) { // recursive indentation?
		String s = "";
		for (int i=0;i<n;i++) s = s + " ";
		s = s + "<semantictype name=\"";
		if (superTypes.isEmpty()) return s + name + "\"/>\n";
		else {
			s = s + name + "\">\n";
			for (SemanticType t: superTypes) {
				s = s + t.toXML(n+2);
			}
			for (int i=0;i<n;i++) s = s + " ";
			return s + "</semantictype>\n";
		}
	}
	
}
